<?php

namespace Shahu\Common\common\qiniu;

class qiniuCommon
{
    /**
     * 判断是否是七牛的图片地址
     * @param $image
     * @return bool
     */
    public static function judgeQiniuImage($image): bool
    {
        //判断是否七牛域名
        $qiniu_domain = [
            'fast.shahuserver.com',
            'fast.shahuserver.cn'
        ];
        $image_params = parse_url($image);
        $host = $image_params['host'] ?? '';
        if (!in_array($host, $qiniu_domain)) {
            return false;
        }
        return true;
    }


    /**
     * 图片瘦身
     * @param $image
     * @return mixed|string
     */
    public static function slimImage($image): mixed
    {
        if (!self::judgeQiniuImage($image)) {
            return $image;
        }
        //判断是否有问号参数
        $query = $image_params['query'] ?? '';
        if (!empty($query)) {
            return $image;
        }
        return $image . '?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|imageslim';
    }


    /**
     * 图片还原
     * @param $image
     * @return array|mixed|string|string[]
     */
    public static function restoreImage($image): mixed
    {
        if (!self::judgeQiniuImage($image)) {
            return $image;
        }
        
        return str_replace('?imageMogr2/auto-orient/format/webp/blur/1x0/quality/60|imageslim', '', $image);
    }
}
